package com.bianguo.android.recyclerlibrary;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;

import com.bianguo.android.recyclerlibrary.adapter.RViewAdapter;
import com.bianguo.android.recyclerlibrary.listener.RViewCreate;

public class RViewHelper<T> {

    private SwipeRefreshLayout swipeRefresh; // 下拉控件
    private SwipeRefreshHelper swipeRefreshHelper; // 下拉刷新的工具类
    private RecyclerView recyclerView; // RecyclerView
    private RViewAdapter<T> adapter; // 适配器
    private RecyclerView.LayoutManager layoutManager; // 布局管理
    private RecyclerView.ItemDecoration itemDecoration; // 条目分割
    private int startPageNumber; // 开始页码
    private boolean isSupportPaging; // 是否支持加载更多
    private SwipeRefreshHelper.SwipeRefreshListener listener; // 下拉刷新、加载更多监听
    private int currentPageNum; // 当前页数


    private RViewHelper(Builder<T> builder) {
        this.swipeRefresh = builder.create.createSwipeRefreshLayout();
        this.recyclerView = builder.create.createRecyclerView();
        this.adapter = builder.create.createRViewAdapter();
        this.layoutManager = builder.create.createLayoutManager();
        this.itemDecoration = builder.create.createItemDecoration();
        this.startPageNumber = builder.create.starPageNumber();
        this.isSupportPaging = builder.create.isSupportPaging();
        this.listener = builder.listener;

        this.currentPageNum = this.startPageNumber;
        int[] colorRes = builder.create.colorRes();
        if (swipeRefresh != null) {
            if (colorRes == null) {
                swipeRefreshHelper = SwipeRefreshHelper.createSwipeRefreshHelper(swipeRefresh);
            } else {
                swipeRefreshHelper = SwipeRefreshHelper.createSwipeRefreshHelper(swipeRefresh, colorRes);
            }
        }
        init();
    }

    private void init() {
        // RecyclerView初始化
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        if (itemDecoration != null) {
            recyclerView.addItemDecoration(itemDecoration);
        }

        recyclerView.setAdapter(adapter);

        // 下拉刷新的操作
        if (swipeRefreshHelper != null) {
            swipeRefreshHelper.setSwipeRefreshListener(new SwipeRefreshHelper.SwipeRefreshListener() {
                @Override
                public void onRefresh() {
                    // 下拉刷新是否 == 重新加载？
                    currentPageNum = startPageNumber; // 重置页码
                    dismissSwipeRefresh();
                    if (listener != null) listener.onRefresh();
                }
            });
        }
    }

    private void dismissSwipeRefresh() {
        if (swipeRefresh != null && swipeRefresh.isRefreshing()) {
            swipeRefresh.setRefreshing(false);
        }
    }

    public RViewAdapter<T> getAdapter() {
        return adapter;
    }

    public static class Builder<T> {

        private final RViewCreate<T> create;
        private final SwipeRefreshHelper.SwipeRefreshListener listener;

        public Builder(RViewCreate<T> create, SwipeRefreshHelper.SwipeRefreshListener listener) {
            this.create = create;
            this.listener = listener;
        }

        public RViewHelper build() {
            return new RViewHelper(this);
        }
    }
}
